package com.edu.hre.common.utils;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;

import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreUser;


// class cung cấp các hàm query parameter, attribute request, attribute session ...
public class RequestUtils {
	
	
	public static class Attribute {
		public static final String AUTHENTICATED = "authen-ok"; 
		public static final String MENU_ROLES = "primary-nav-";
		public static final String USER_INFO = "user-info";
		public static final String HreUserRoleScreen = "HreUserRoleScreen"; 
		public static final String EMPLOYEE_INFO = "emp-info";
		public static final String URL_BEFORE_LOGIN = "url-before-login";
		public static final String ROLES_LEAVE_APPROVAL = "roles-leave-approval";
		public static final String LEAVE_APPROVAL_NOTIFICATION = "leave-approval-ntf-";
		public static final String TAKE_SURVEY_NOTIFICATION = "take-survey-ntf-";
		public static final String LIST_SUB_UNITS_STRUCTURE = "subunits-structures-"; 
	}
	public static class Parameter {
		public static final String ACCOUNT = "acc";
		public static final String ACTION = "act";
		public static final String ID = "id";
		//....
	}
	
	public static boolean isLogout(HttpServletRequest request){
		String s = request.getParameter(Parameter.ACTION);
		return "logout".equalsIgnoreCase(s);
	}
	
	public static String getWelcomeName(HttpServletRequest request){
		String welcomeName = null;
		HreEmployee employee = (HreEmployee) request.getSession().getAttribute(RequestUtils.Attribute.EMPLOYEE_INFO);
		if(employee != null){
			welcomeName = employee.getEmpLastname() + " "
				+ (employee.getEmpMiddleName() == null ? "" :  employee.getEmpMiddleName()
				+ " " + employee.getEmpFirstname()) ;
		} else {
			HreUser user = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
			welcomeName = user.getUserName();
		} 
		return welcomeName;
	}
	
	public static int getIDParameter(HttpServletRequest request, int defaultValue){
		try { 
			return Integer.parseInt((String)request.getAttribute(Parameter.ID));
		} catch (Exception e) { 
			try {
				return Integer.parseInt(request.getParameter(Parameter.ID));
			} catch (Exception e2) {
				return defaultValue;
			}
		} 
	} 
	
	public static String getAccountParameter(HttpServletRequest request, String defaultValue){
		String tmp = (String)request.getAttribute(Parameter.ACCOUNT);
		if(StringUtils.isNotEmpty(tmp)){
			return tmp;
		}
		tmp = (String)request.getParameter(Parameter.ACCOUNT);
		if(StringUtils.isNotEmpty(tmp)){
			return tmp;
		}
		return defaultValue;
	}
	
	public static String getParameterStr (HttpServletRequest request, String paramName, String defaultValue) {

		String result = request.getParameter(paramName);
		
		if (result == null) {
			return defaultValue;
		}
		
		return result;
	}
	
	public static int getParameterInt (HttpServletRequest request, String paramName, int defaultValue) {

		String result = request.getParameter(paramName);
		
		if (result == null) {
			return defaultValue;
		}
		
		return CommonUtils.parseInt(result, defaultValue);
	}
}
